﻿/*
 * Created by SharpDevelop.
 * User: IEscarro
 * Date: 9/1/2009
 * Time: 9:59 AM
 * 
 * To change this template use Tools | Options | Coding | Edit Standard Headers.
 */

using System;
using NHibernate;
using System.Collections.Generic;
using System.Data;
using Buaya.Model.Dao.Interface;

namespace Buaya.Model.Dao.NHibernate
{
	/// <summary>
	/// Description of NHibernateUserDao.
	/// </summary>
	public class NHibernateUserDao : IUserDao
	{
		public NHibernateUserDao()
		{
		}
		
		public void SaveOrUpdate(User user)
		{
			ISession session = NHibernateHelper.OpenSession();
			session.SaveOrUpdate(user);
			session.Flush();
		}
		
		public void Delete(User user)
		{
			ISession session = NHibernateHelper.OpenSession();
			session.Delete(user);
			session.Flush();
		}
		
		public User ReadByPk(int id)
		{
			return NHibernateHelper.OpenSession().Load<User>(id);
		}
		
		public User ReadByNameAndPassword(string name, string password)
		{
			IQuery query = NHibernateHelper.OpenSession().CreateQuery("from User where Name = :name and Pass = :password");
			query.SetString("name", name);
			query.SetString("password", password);
			return query.UniqueResult<User>();
		}
		
		public IList<User> FindAll()
		{
			return NHibernateHelper.OpenSession().CreateCriteria(typeof(User)).List<User>();
		}
	}
}
